package com.meitq.user.common.config.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;

import javax.net.ssl.HostnameVerifier;
import java.nio.charset.Charset;


@Configuration
@EnableConfigurationProperties(HttpProperties.class)
public class HttpClientConfig {

    @Autowired
    private HttpProperties httpProperties;

    @Bean
    @ConditionalOnClass(name = "org.apache.http.conn.HttpClientConnectionManager")
    public HttpClientConnectionManager httpClientConnectionManager() {

        HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(PublicSuffixMatcherLoader.getDefault());
        LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);

        // Create a connection manager with custom configuration.
        final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build());

        // 最大连接数
        connManager.setMaxTotal(httpProperties.getMaxConnection());
        // 每个路由基础的连接
        connManager.setDefaultMaxPerRoute(httpProperties.getDefaultMaxPerRoute());

        ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8"))
                .build();
        connManager.setDefaultConnectionConfig(connectionConfig);
        SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(httpProperties.getSoTimeout()).build();
        connManager.setDefaultSocketConfig(socketConfig);
        return connManager;
    }

    @Bean
    @ConditionalOnClass(name = "org.apache.http.client.HttpClient")
    public HttpClient httpClient(HttpClientConnectionManager httpClientConnectionManager, RequestConfig requestConfig) {
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager).setDefaultRequestConfig(requestConfig)
                .build();
        return httpClient;
    }

    @Bean("defaultHttpRequestConfig")
    public RequestConfig requestConfig() {
        // 设置链接和读取超时
        RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(httpProperties.getConnectTimeout()).setConnectionRequestTimeout(httpProperties.getConnectionTimeout())
                .setSocketTimeout(httpProperties.getSoTimeout()).build();
        return defaultRequestConfig;
    }





}
